using UnityEngine;
using UnityEditor;
using Framework.Core;
using System.Collections.Generic;
//https://blog.csdn.net/weixin_43853077/article/details/126459315
namespace Framework
{
    public class CustomPoolInfo : EditorWindow
    {
        public static void ShowWindow()
        {
            var window = GetWindow<CustomPoolInfo>(true, "对象池信息", true);
            window.minSize = new Vector2(500, 200);
            window.maxSize = new Vector2(900, 800);
            window.autoRepaintOnSceneChange = true;
            // window.Show();
        }
        private Vector2 scrollViewPos = Vector2.zero;
        private List<PoolInfo> _poolList = new List<PoolInfo>();
        private int _selectIndex = 2;
        private bool _repaint;
        private int _hashId;
        private void OnGUI()
        {
            EditorGUILayout.BeginHorizontal("box");
            if (GUI.Toggle(new Rect(0, 0, 100, 30), _selectIndex == 1, "所有"))
            {
                _selectIndex = 1;
                _repaint = true;
            }
            if (GUI.Toggle(new Rect(100, 0, 100, 30), _selectIndex == 2, "使用中"))
            {
                _selectIndex = 2;
                _repaint = true;
            }

            EditorGUILayout.EndHorizontal();

            GUILayout.Space(20);

            EditorGUILayout.BeginVertical("box");

            EditorGUILayout.BeginHorizontal();
            if (_selectIndex == 1)
            {
                EditorGUILayout.LabelField("使用中/总量", EditorStyles.boldLabel);
            }
            else
            {
                EditorGUILayout.LabelField("数量", EditorStyles.boldLabel);
            }
            EditorGUILayout.LabelField("目标", EditorStyles.boldLabel);
            EditorGUILayout.EndHorizontal();
            //GUILayout.FlexibleSpace();
            scrollViewPos = GUILayout.BeginScrollView(scrollViewPos, false, false);
            _poolList.Clear();
            PoolInfo.Foreach(_poolList);
            if (PoolInfo.ID != _hashId)
            {
                _hashId = PoolInfo.ID;
                _repaint = true;
            }
            if (_selectIndex == 2)
            {
                var len = _poolList.Count;
                while (len-- > 0)
                {
                    if (_poolList[len].ActiveCount < 1)
                    {
                        _poolList.RemoveAt(len);
                    }
                }
                _poolList.Sort(sort);
            }else
            {
                _poolList.Sort(sort2);
            }

            foreach (var item in _poolList)
            {
                string title = string.Empty;
                if (_selectIndex == 2)
                {
                    title = string.Format("{0}\t{1} - {2}", item.ActiveCount, item.Name, item.Type);
                }
                else if (_selectIndex == 1)
                {
                    title = string.Format("{0}/{1}\t{2} - {3}", item.ActiveCount, item.ActiveCount + item.UnusedCount, item.Name, item.Type);
                }

                GUILayout.Label(title);
            }
            GUILayout.EndScrollView();
            EditorGUILayout.EndVertical();
            if (_repaint)
            {
                _repaint = false;
                this.Repaint();
            }
        }
        private int sort(PoolInfo a, PoolInfo b)
        {
            return b.ActiveCount - a.ActiveCount;
        }
        private int sort2(PoolInfo a, PoolInfo b)
        {
            return (b.ActiveCount + b.UnusedCount) - (a.ActiveCount + a.UnusedCount);
        }
    }
}